使用boost asio创建一个iostream,指定ip和端口

您所在的位置:网站首页 瑞金archive our own 使用boost asio创建一个iostream,指定ip和端口

使用boost asio创建一个iostream,指定ip和端口

#使用boost asio创建一个iostream,指定ip和端口| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

I have a problem concerning boost asio libraries. I successfully tried to create a socket between a client and a server, this involves creation of resolvers in order to specify ip and port to the server (the server only requires port) and other objects, but, most importantly, it is necessary to use write and read_some as functions to read and write from/in the socket. I would really appreciate to use a stream, and this is possible in boost asio, but that's strange... In almost all examples using streams, to create a server it is necessary to provide port, ok, let's talk about the client... client side, it is necessary to use the iostream constructor to specify coordinates for connecting the stream, here's the code:

tcp::iostream() s(argv[1], "daytime");

Well, I don't really understand what is passed in the first parameter and really don't know what daytime might ever represent... Basically, here, I'm telling: "Hey stream, you must connect to this server..." but how can I specify ip and port of that server? Note that, on the opposite, everything is almost clear server side:

boost::asio::io_service io_s; tcp::acceptor acc(io_s, tcp::endpoint(tcp::v4(), 1950)); for (;;) { tcp::iostream stream; acc.accept(*stream.rdbuf()); stream message;

You would potentially create one archive and use it throughout, especially for outbound. For inbound you may have issues if you get a streaming error as it will break your archive.

You can use a text archive instead of a binary one.

The boost archive will automatically put in header information so it knows when an object is complete and will only return to you once it has a complete object or something has broken.

Note: primitive types, eg std::string and even vector< int > etc. are automatically handled by an archive. Your own classes will need special overloads as to how to stream them. You should read boost::archive documentation.

Note: You can connect the archive object to the stream before the stream has been opened. The archive works around the streambuf object which does not change dependent on the stream opening successfully.

Creating without no_header would be an issue though as the archives immediately try to use the stream on construction (to read or write their header)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3